import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/new_health/new_health_record_list_view_model.dart';

import '../../res/colors.dart';
import '../../widgets/common_widget.dart';

class NewHealthRecordListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _NewHealthRecordListState();
}

class _NewHealthRecordListState extends BaseMvvmListState<
    NewHealthRecordListViewModel, NewHealthRecordListPage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('体征记录'),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(
                Icons.search,
                color: Colors.blue,
              ))
        ],
      ),
      body: loadingContentWidget,
      key: key,
      endDrawer: endWidget(),
    );
  }

  Widget endWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppBar(
          title: Text('筛选'),
          leading: Container(),
          actions: [Container()],
        ),
        SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CommonText('长者姓名/房间号'),
              SizedBox(
                height: 10,
              ),
              NoBorderInput(
                      hintText: '输入长者姓名/房间号',
                      maxLines: 1,
                      text: viewModel.searchWords,
                      textInputAction: TextInputAction.search,
                      onChanged: (str) {
                        viewModel.searchWords = str;
                      },
                      onSubmitted: (str) {
                        viewModel.requestRefresh();
                      })
                  .addToContainer(
                      decoration: BoxDecoration(
                          color: Colours.bg_color,
                          borderRadius: 6.borderRadius),
                      padding:
                          EdgeInsets.symmetric(horizontal: 12, vertical: 8)),
              SizedBox(
                height: 16,
              ),
              TitleValueWidget(
                      title: "登记时间",
                      value: vm?.startDateStr ?? "",
                      padding: EdgeInsets.zero,
                      arrow: Icon(Icons.keyboard_arrow_right_sharp))
                  .onTap(() {
                viewModel.selectStartDate(click: (str) {
                  viewModel.requestRefresh();
                });
              }),
              SizedBox(
                height: 16,
              ),
              TitleValueWidget(
                      title: "楼宇",
                      value: vm?.selectedBuilding?.name ?? "",
                      padding: EdgeInsets.zero,
                      arrow: Icon(Icons.keyboard_arrow_right_sharp))
                  .onTap(() {
                viewModel.chooseBuilding();
              }),
              SizedBox(
                height: 16,
              ),
              TitleValueWidget(
                      title: "楼层",
                      value: vm?.selectedFloor?.name ?? "",
                      padding: EdgeInsets.zero,
                      arrow: Icon(Icons.keyboard_arrow_right_sharp))
                  .onTap(() {
                viewModel.chooseFloor();
              }),
              SizedBox(
                height: 12,
              ),
              CommonText('登记项目'),
              SizedBox(
                height: 12,
              ),
              Wrap(
                spacing: 10,
                runSpacing: 10,
                children: (vm?.signItemList ?? []).map((e) {
                  return CommonText(e.name,textColor: viewModel.selectedItems.contains(e)? Colors.white:Colors.grey).addToContainer(
                      decoration: BoxDecoration(
                          color: viewModel.selectedItems.contains(e)? Colours.app_main:Colors.white,
                          border: Border.all(color: viewModel.selectedItems.contains(e)? Colours.app_main:Colors.grey),
                          borderRadius: 4.borderRadius),
                      padding:
                          EdgeInsets.symmetric(horizontal: 12, vertical: 5)).onTap(() {
                            setState(() {
                              if(viewModel.selectedItems.contains(e)){
                                viewModel.selectedItems.remove(e);
                              }else{
                                viewModel.selectedItems.add(e);
                              }
                            });
                            viewModel.delayRequest();
                  });
                }).toList(),
              )
            ],
          ),
        ).setWeight(1),
        Divider(
          height: 1,
        ),
        Row(
          children: [
            TextButton(
              onPressed: () {
                Go().pop();
                viewModel.reset();
              },
              child: CommonText('重置', textColor: Colors.grey),
              style: ButtonStyle(
                shape: WidgetStateProperty.all(RoundedRectangleBorder()),
                minimumSize: WidgetStateProperty.all(Size.fromHeight(49)),
              ),
            ).setWeight(1),
            VerticalDivider(
              width: 1,
            ),
            TextButton(
              onPressed: () {
                Go().pop();
                viewModel.requestRefresh();
              },
              child: CommonText('确定', textColor: Colors.white),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder()),
                  minimumSize: WidgetStateProperty.all(Size.fromHeight(49)),
                  backgroundColor: WidgetStateProperty.all(Colours.app_main)),
            ).setWeight(1),
          ],
        )
      ],
    ).addToContainer(
        width: context.screenSize.width * 0.75,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 10.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      alignment: Alignment.center,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CommonText('长者姓名：${item.customerName}'),
                SizedBox(
                  width: 12,
                ),
                CommonText('${item.bedNo}',
                    textColor: Colors.blue, textSize: 14.5)
              ],
            ),
            CommonText('年龄：${item.age}', textSize: 15),
            CommonText('性别：${item.sex}', textSize: 15),
            CommonText('测量时间：${item.collectionTime}', textSize: 15),
            CommonText('登记时间：${item.createdDate}', textSize: 15),
            CommonText('登记人：${item.createdByName}', textSize: 15),
          ].insertWidget(SizedBox(
            height: 12,
          )),
        ),
        CommonText(item.dataType).setLocation(right: 20, top: 0),
        CommonText(item.numberConcat,
                textSize: 22,
                textColor: Colors.blue,
                fontWeight: FontWeight.bold)
            .setLocation(right: 20)
      ],
    ).addToContainer(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12));
  }

  @override
  Widget get separatorDivider => Gaps.line;

  @override
  NewHealthRecordListViewModel createViewModel() {
    return NewHealthRecordListViewModel(context);
  }
}
